/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TestPrincipal;

import analisis.IntraProceduralAnalysis;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import reticulado.PointsToGraph;
import soot.Body;
import soot.Local;
import soot.PhaseOptions;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.toolkits.callgraph.CHATransformer;
import soot.options.Options;
import soot.toolkits.graph.BriefUnitGraph;
import soot.toolkits.graph.UnitGraph;
import static org.junit.Assert.*;

/**
 *
 * @author Santiago Avendaño
 */
public class TestEj2 {

    private static SootClass c;
    private static final String campoNext = "next";
    private static final String campoPrev = "prev";
    private static Local variablex;
    private static Local variabley;
    private static Local variablez;

    public TestEj2() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        Options.v().set_keep_line_number(true);
        PhaseOptions.v().setPhaseOption("jb", "use-original-names:true");
        c = Scene.v().loadClassAndSupport("CasosTestIntra.D");
        c.setApplicationClass();
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    //@Ignore
    public void test_m01() {
        System.out.println("TEST 01");
        SootMethod m = c.getMethodByName("m_01");
        Body b = m.retrieveActiveBody();
        setearVariables(b);
        System.out.println("Cuerpo del metodo");
        System.out.println(b.toString());
        UnitGraph g = new BriefUnitGraph(b);
        IntraProceduralAnalysis an = new IntraProceduralAnalysis(g, null, null, null, null);
        PointsToGraph res = an.getResult();
        System.out.println(res.toString());
        assertTrue("R is empty? SI", res.getEjesR().isEmpty());
        assertFalse("W is empty? NO", res.getEjesW().isEmpty());
    }

    @Test
    //@Ignore
    public void test_m02() {
        SootMethod m = c.getMethodByName("m_02");
        Body b = m.retrieveActiveBody();
        setearVariables(b);
        System.out.println("Cuerpo del metodo");
        System.out.println(b.toString());
        UnitGraph g = new BriefUnitGraph(b);
        IntraProceduralAnalysis an = new IntraProceduralAnalysis(g, null, null, null, null);
        PointsToGraph res = an.getResult();
        System.out.println(res.toString());
        assertFalse("R is empty? NO", res.getEjesR().isEmpty());
    }

    @Test
    //@Ignore
    public void test_m03() {
        SootMethod m = c.getMethodByName("m_03");
        Body b = m.retrieveActiveBody();
        setearVariables(b);
        System.out.println("Cuerpo del metodo");
        System.out.println(b.toString());
        UnitGraph g = new BriefUnitGraph(b);
        IntraProceduralAnalysis an = new IntraProceduralAnalysis(g, null, null, null, null);
        PointsToGraph res = an.getResult();
        System.out.println(res.toString());
        assertTrue("R is empty? SI", res.getEjesR().isEmpty());
        //assertTrue("x may-alias y ? Si", res.mayAlias(variablex, variabley));
    }

    @Test
    //@Ignore
    public void test_m04() {
        SootMethod m = c.getMethodByName("m_04");
        Body b = m.retrieveActiveBody();
        setearVariables(b);
        System.out.println("Cuerpo del metodo");
        System.out.println(b.toString());
        UnitGraph g = new BriefUnitGraph(b);
        IntraProceduralAnalysis an = new IntraProceduralAnalysis(g, null, null, null, null);
        PointsToGraph res = an.getResult();
        System.out.println(res.toString());
        assertTrue("R is empty? SI", res.getEjesR().isEmpty());
        //assertTrue("x may-alias y ? Si", res.mayAlias(variablex, variabley));
    }

    @Test
    //@Ignore
    public void test_m05() {
        SootMethod m = c.getMethodByName("m_05");
        Body b = m.retrieveActiveBody();
        setearVariables(b);
        System.out.println("Cuerpo del metodo");
        System.out.println(b.toString());
        UnitGraph g = new BriefUnitGraph(b);
        IntraProceduralAnalysis an = new IntraProceduralAnalysis(g, null, null, null, null);
        PointsToGraph res = an.getResult();
        System.out.println(res.toString());
        assertTrue("R is empty? SI", res.getEjesR().isEmpty());
        //assertTrue("x may-alias y ? Si", res.mayAlias(variablex, variabley));
    }

    private void setearVariables(Body b) {
        for (Local v : b.getLocals()) {
            if (v.getName().equals("x")) {
                variablex = v;
            }
            if (v.getName().equals("y")) {
                variabley = v;
            }
            if (v.getName().equals("z")) {
                variablez = v;
            }
        }
    }
}
